#ifndef _RELAY_H
#define _RELAY_H

#include "stdint.h"
#include "stdbool.h"
#include "fw/include/mgos_sys_config.h"
#include "frozen/frozen.h"
#include "fw/include/mgos_timers.h"
#include "string.h"
#include "fw/include/mgos_gpio.h"
#include "common/json_utils.h"
#include "mgos_rpc.h"
#include "fw/include/mgos.h"

#define TZ 8
#define ON false
#define OFF true
#define TICK 100;

struct mgos_config_r
{
    int idx;
    int enable;
    int pulse;
    int on;
    int off;
    char *title;
    int priority;
    char *schedule;
};

struct Relay
{
    uint8_t idx;
    uint8_t pin;
    bool enable;
    bool manual;
    bool state; // 是开启命令，不是pin打开
    bool state_last;
    bool pulse; // 持续，脉冲？
    uint32_t t_on;
    uint32_t t_duty;
    uint32_t manual_end;
    //bool busy;
    uint32_t duration;
    uint32_t schedule[12];
};

void r_init(uint8_t idx, struct Relay *r_t);
void r_scan(struct Relay *r_t);
void cb_myRpcManual(struct mg_rpc_request_info *ri, const char *args,
                    const char *src, void *user_data);
void state_update(uint8_t idx, bool state);

#endif